﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class SacrificeList_DAL : ISacrificeList
    {
        #region ISacrificeList 成员

        public db.ParameterArry[] Parameter(SacrificeList_Model model)
        {
            db.ParameterArry[] Parameter = new db.ParameterArry[11];


            Parameter[0].objValue = model.Id;
            Parameter[0].parName = "@id";
            Parameter[0].dbType = SqlDbType.Int;

            Parameter[1].objValue = model.UserName;
            Parameter[1].parName = "@UserName";
            Parameter[1].dbType = SqlDbType.VarChar;

            Parameter[2].objValue = model.TrueName;
            Parameter[2].parName = "@TrueName";
            Parameter[2].dbType = SqlDbType.VarChar;

            Parameter[3].objValue = model.Content;
            Parameter[3].parName = "@Content";
            Parameter[3].dbType = SqlDbType.VarChar;

            Parameter[4].objValue = model.PhotoId;
            Parameter[4].parName = "@PhotoId";
            Parameter[4].dbType = SqlDbType.Int;

            Parameter[5].objValue = model.Addtime;
            Parameter[5].parName = "@Addtime";
            Parameter[5].dbType = SqlDbType.VarChar;

            Parameter[6].objValue = model.NetIp;
            Parameter[6].parName = "@NetIp";
            Parameter[6].dbType = SqlDbType.VarChar;

            Parameter[7].objValue = model.ChannelId;
            Parameter[7].parName = "@channelId";
            Parameter[7].dbType = SqlDbType.VarChar;

            Parameter[8].objValue = model.TypeId;
            Parameter[8].parName = "@TypeId";
            Parameter[8].dbType = SqlDbType.Int;


            Parameter[9].objValue = model.Classid;
            Parameter[9].parName = "@classid";
            Parameter[9].dbType = SqlDbType.Int;

            Parameter[10].objValue = model.UserId;
            Parameter[10].parName = "@UserId";
            Parameter[10].dbType = SqlDbType.Int;



            return Parameter;
        }

        public int Add(SacrificeList_Model model)
        {
            db.ParameterArry[] par = Parameter(model);
            db conn = new db();
            int result = 0;
            try
            {
                string sqlstr = "Insert into SacrificeList(UserName,TrueName,Content,PhotoId,NetIp,channelId,TypeId,classid,UserId) ";
                sqlstr += " values({0},{1},{2},{3},{4},{5},{6},{7},{8})";
                sqlstr = string.Format(sqlstr, par[1].parName, par[2].parName, par[3].parName, par[4].parName, par[6].parName, par[7].parName, par[8].parName, par[9].parName, par[10].parName);
                result = conn.ExcSql(sqlstr, par);
            }
            catch
            {
                result = -1;
            }
            finally
            {
                conn.close();
            }

            return result;
        }

        public DataTable GetDataTable(string filter)
        {
            if (filter != "")
            {
                filter = " Where " + filter;
            }
            string sqlstr = "Select * from SacrificeList " + filter;
            db Conn = new db();
            DataTable dt = Conn.GetSql_DataTable(sqlstr);
            return dt;
        }

        /// <summary>
        /// 获取TelNumber集合[分页使用]
        /// </summary>
        /// <param name="PageSize">页大小</param>
        /// <param name="pageindex">页数[从1开始]</param>
        /// <param name="filter">其他条件[eg. ID=0 ]</param>
        /// <returns></returns>
        public DataTable GetDataTable(int PageSize, int pageindex, string filter)
        {
            string strsql = string.Format("SELECT TOP {0} * FROM SacrificeList WHERE {1} and [ID] NOT IN (SELECT TOP {2} [ID] FROM SacrificeList WHERE {1} ORDER BY id DESC ) ORDER BY id DESC "
                , PageSize, filter, ((pageindex - 1) * PageSize));
            db dbc = new db();
            DataTable dt = dbc.GetSql_DataTable(strsql);
            dbc.close();
            dbc.Dispose();
            return dt;
        }





        public SacrificeList_Model GetModel(DataRow dr)
        {
            SacrificeList_Model Md = new SacrificeList_Model();
            if (dr["ID"] != null) { Md.Id = StringHelper.isNumber_int(dr["id"].ToString()); }
            if (dr["UserName"] != null) { Md.UserName = (dr["UserName"].ToString()); }
            if (dr["UserId"] != null) { Md.UserId = StringHelper.isNumber_int(dr["UserId"].ToString()); }
            if (dr["TrueName"] != null) { Md.TrueName = (dr["TrueName"].ToString()); }
            if (dr["Content"] != null) { Md.Content = (dr["Content"].ToString()); }
            if (dr["PhotoId"] != null) { Md.PhotoId = StringHelper.isNumber_int(dr["PhotoId"].ToString()); }
            if (dr["Addtime"] != null) { Md.Addtime = (dr["Addtime"].ToString()); }
            if (dr["NetIp"] != null) { Md.NetIp = (dr["NetIp"].ToString()); }
            if (dr["channelId"] != null) { Md.ChannelId = (dr["channelId"].ToString()); }
            if (dr["TypeId"] != null) { Md.TypeId = StringHelper.isNumber_int(dr["TypeId"].ToString()); }
            if (dr["classid"] != null) { Md.Classid = StringHelper.isNumber_int(dr["classid"].ToString()); }
            return Md;



        }


        public int GetReDataRecord(string filter)
        {
            if (filter != "")
            {
                filter = " Where " + filter;
            }
            string sqlstr = "Select Count(id) from SacrificeList " + filter;
            db Conn = new db();
            return (int)Conn.GetOneFile(sqlstr);
        }

        #endregion
    }
}
